package com.sxt.pro.controller;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxt.pro.pojo.Loan;
import com.sxt.pro.service.LoanService;
import com.sxt.pro.vo.LoanVo;
import com.sxt.sys.common.DataGridView;
import com.sxt.sys.common.ResultObj;
import com.sxt.sys.common.WebUtils;


/**  

* @Description: 贷款类型控制层

* @author xinxiaokang  

* @date 2021年3月15日 下午3:17:43

*/
@RestController
@RequestMapping("/loan")
public class LoanController {

	@Autowired
	private LoanService loanService;
	
	/**
	 * 查询贷款类型及分页列表
	 * @param loanVo
	 * @return
	 */
	@RequestMapping("/loadAllLoan")
	public DataGridView loadAllLoan(LoanVo loanVo){
		//存入网点编号和用户名放入session中
		 int banknbr = (int) WebUtils.getSession().getAttribute("banknbr");
		 loanVo.setBanknbr(banknbr);
		//初始化 当前页、每页显示条数
		IPage<Loan> page = new Page<>(loanVo.getPage(), loanVo.getLimit());
		//封装对象查询条件
		QueryWrapper<Loan> queryWrapper = new QueryWrapper<Loan>();
		//筛选条件：网点编号
		queryWrapper.eq(loanVo.getBanknbr()!=null&&loanVo.getBanknbr()!=0,"banknbr",loanVo.getBanknbr());
		//筛选条件:贷款类型名称
		queryWrapper.like(StringUtils.isNotBlank(loanVo.getLoanname()), "loanname", loanVo.getLoanname());
		//筛选条件：大于开始时间
		queryWrapper.ge(loanVo.getStartTime()!=null, "createtime", loanVo.getStartTime());
		//筛选条件：小于结束时间
		queryWrapper.le(loanVo.getEndTime()!=null, "createtime", loanVo.getEndTime());
		queryWrapper.orderByDesc("createtime");
		//获取分页数据
		this.loanService.page(page, queryWrapper);
		return new DataGridView(page.getTotal(), page.getRecords());
	}
	
	/**
	 * 添加贷款类型
	 * @param loanVo
	 * @return
	 */
	@RequestMapping("addLoan")
	public ResultObj addLoan(LoanVo loanVo) {
		//添加时，网点默认
		int banknbr = (int) WebUtils.getSession().getAttribute("banknbr");
		loanVo.setBanknbr(banknbr);
		try {
			loanVo.setCreatetime(new Date());
			this.loanService.save(loanVo);
			return ResultObj.ADD_SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ResultObj.ADD_ERROR;
		}
	}

	/**
	 * 修改贷款类型
	 * @param loanVo
	 * @return
	 */
	@RequestMapping("updateLoan")
	public ResultObj updateLoan(LoanVo loanVo) {
		try {
			this.loanService.updateById(loanVo);
			return ResultObj.UPDATE_SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ResultObj.UPDATE_ERROR;
		}
	}

	/**
	 * 删除贷款类型
	 * @param id
	 * @return
	 */
	@RequestMapping("deleteLoan")
	public ResultObj deleteLoan(Integer id) {
		try {
			this.loanService.removeById(id);
			return ResultObj.DELETE_SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ResultObj.DELETE_ERROR;
		}
	}
	

	
	/**
	 * 查询贷款类型下拉列表  根据网点编号
	 * @return
	 */
	@RequestMapping("loadAllLoanForSelect")
	public DataGridView loadAllLoanForSelect(){
		Loan loan = new Loan();
		//存入网点编号和用户名放入session中
		 int banknbr = (int) WebUtils.getSession().getAttribute("banknbr");
		 loan.setBanknbr(banknbr);
		//封装对象查询条件
		QueryWrapper<Loan> queryWrapper=new QueryWrapper<>();
		//筛选条件：网点编号
		 queryWrapper.eq(loan.getBanknbr()!=null&&loan.getBanknbr()!=0,"banknbr",loan.getBanknbr());
		List<Loan> list = this.loanService.list(queryWrapper);
		return new DataGridView(list);
	}
	
}
